import random

task_set = set()
worker_set = set()


def erase_geo_precision(geo, level):
    template = "{:."+str(level)+"f}"
    return (template.format(float(geo[0])), template.format(float(geo[1])))


def choose_sg_worker_task():
    city = "sg"
    task_count = 1
    worker_count = 1
    region = (103.80, 1.26, 103.92, 1.34)
    workers = []
    tasks = []
    worker = open('worker_'+city+'.csv', 'w')
    task = open('task_'+city+'.csv', 'w')
    with open('checkin_'+city+'.csv') as checkin:
        for line in checkin.readlines():
            tokens = line.split(",")
            if len(tokens) < 12:
                continue
            geos = (float(tokens[0]), float(tokens[1]))
            if region[1] < float(geos[0]) < region[3] and region[0] < float(geos[1]) < region[2]:
                if tokens[11] == "Office":
                    geos = erase_geo_precision(geos, 4)
                    if geos not in worker_set:
                        worker_set.add(geos)
                        workers.append(
                            (str(worker_count), tokens[0], tokens[1]))
                        worker_count += 1
                elif tokens[-2] == "Subway":
                    geos = erase_geo_precision(geos, 4)
                    if geos not in task_set:
                        task_set.add(geos)
                        tasks.append(
                            (str(task_count), tokens[0], tokens[1]))
                        task_count += 1
    random.shuffle(workers)
    random.shuffle(tasks)
    for i in range(len(workers)):
        worker.write(','.join(workers[i])+'\n')
    for i in range(len(tasks)):
        task.write(','.join(tasks[i])+'\n')
    worker.close()
    task.close()


def choose_ld_worker_task():
    city = "ld"
    task_count = 1
    worker_count = 1
    region = (-0.234, 51.48925199999999, -0.05, 51.55)
    worker = open('worker_'+city+'.csv', 'w')
    task = open('task_'+city+'.csv', 'w')
    workers = []
    tasks = []
    with open('checkin_'+city+'.csv') as checkin:
        for line in checkin.readlines():
            tokens = line.split(",")
            if len(tokens) < 12:
                continue
            geos = (float(tokens[0]), float(tokens[1]))
            if region[1] < float(geos[0]) < region[3] and region[0] < float(geos[1]) < region[2]:
                if tokens[11] == "Office":
                    geos = erase_geo_precision(geos, 4)
                    if geos not in worker_set:
                        worker_set.add(geos)
                        workers.append(
                            (str(worker_count), tokens[0], tokens[1]))
                        worker_count += 1
                elif tokens[-2] == "Subway":
                    geos = erase_geo_precision(geos, 4)
                    if geos not in task_set:
                        task_set.add(geos)
                        tasks.append(
                            (str(task_count), tokens[0], tokens[1]))
                        task_count += 1
    random.shuffle(workers)
    random.shuffle(tasks)
    for i in range(len(workers)):
        worker.write(','.join(workers[i])+'\n')
    for i in range(len(tasks)):
        task.write(','.join(tasks[i])+'\n')
    worker.close()
    task.close()


if __name__ == "__main__":
    choose_sg_worker_task()
